package com.beiing.tianshuai.tianshuai.message.model;

import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.ConsumeMarksBean;
import com.beiing.tianshuai.tianshuai.entity.NewFriendMessageBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/8/8 0008.
 */

public class NewFriendMessageModel implements NewFriendMessageModelImpl{

    private static final String                         TAG = "NewFriendMessageModel";
    private NewFriendMessageModel.OnRequestListener     mListener;

    public NewFriendMessageModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getNewFriendMessage(String uid) {
        Observable<NewFriendMessageBean> loginResult = DataManager.getNewFriendMessage(
                uid
        );
        Observable<NewFriendMessageBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<NewFriendMessageBean>() {

            @Override
            public void onNext(@NonNull NewFriendMessageBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getRefuseAddFriend(String id, final String fromId, final String toId) {
        Observable<CommonBean> loginResult = DataManager.getRefuseResult(
                id,
                fromId,
                toId
        );
        Observable<CommonBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                if (mListener != null) {
                    mListener.onRefuseSuccess(bean, fromId);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getAgreeAddFriend(String id, final String fromId, final String toId) {
        Observable<CommonBean> loginResult = DataManager.getAgreeRequest(
                id,
                fromId,
                toId
        );
        Observable<CommonBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                if (mListener != null) {
                    mListener.onAgreeSuccess(bean, fromId);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void consumeNewFriendsMarks(String uid) {
        DisposableObserver<ConsumeMarksBean> observer = new DisposableObserver<ConsumeMarksBean>() {
            @Override
            public void onNext(@NonNull ConsumeMarksBean consumeMarksBean) {
                if (mListener != null){
                    mListener.onConsumeSuccess(consumeMarksBean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null){
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getConsumeNewFriendMarksResult(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public interface OnRequestListener {
        void onSuccess(NewFriendMessageBean bean);
        void onAgreeSuccess(CommonBean bean, String fromId);
        void onRefuseSuccess(CommonBean bean, String fromId);
        void onConsumeSuccess(ConsumeMarksBean bean);
        void onError(Throwable throwable);
    }
}
